﻿Imports System.Data.OleDb

Public Class EditQuizQuestion
    Inherits System.Web.UI.Page
    Dim index As Int32
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try


            index = 0
            If (Not Page.IsPostBack) Then
                If (Request.QueryString("QuizQuestionId") <> Nothing) Then
                    pnlFields.Visible = True

                    Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                    Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizQuestionId(Convert.ToInt32(Request.QueryString("QuizQuestionId")))

                    If dr.HasRows Then
                        dr.Read()
                        If Not IsDBNull(dr("Quiz_QuestionContent")) Then txtQuestion.Text = dr("Quiz_QuestionContent")
                        If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer1")) Then txtPossibleAnswer1.Text = dr("Quiz_QuestionPossibleAnswer1")
                        If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer2")) Then txtPossibleAnswer2.Text = dr("Quiz_QuestionPossibleAnswer2")
                        If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer3")) Then txtPossibleAnswer3.Text = dr("Quiz_QuestionPossibleAnswer3")
                        If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer4")) Then txtPossibleAnswer4.Text = dr("Quiz_QuestionPossibleAnswer4")
                        If Not IsDBNull(dr("Quiz_QuestionAnswerDescription")) Then txtAnswerExplenation.Text = dr("Quiz_QuestionAnswerDescription")
                        If Not IsDBNull(dr("Quiz_QuestionCorrectAnswer")) Then drpPossibleAnswers.SelectedValue = dr("Quiz_QuestionCorrectAnswer")
                    End If

                Else
                    pnlFields.Visible = False
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub



    Protected Function getIndex()
        Try

        index = index + 1
            Return "Question " + index.ToString()
        Catch ex As Exception
            Return Nothing
        End Try

    End Function

    Protected Function getPicture(ByVal url As String)
        Try

            Return "../" + url

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Sub btnAddQuestion_Click(sender As Object, e As EventArgs) Handles btnAddQuestion.Click
        Try


            Dim myGuid As New Guid
            myGuid = Guid.NewGuid()

            Dim fileIsAnImage As Boolean = False
            Dim fileName As String
            Dim myFileName As String = String.Empty

            If (fuPictureUrl.HasFile) Then

                fileName = Server.HtmlEncode(fuPictureUrl.FileName)
                Dim extension As String = System.IO.Path.GetExtension(fileName)

                If (extension = ".jpg") Then
                    myFileName = myGuid.ToString() + ".jpg"
                    fileIsAnImage = True
                ElseIf (extension = ".png") Then
                    myFileName = myGuid.ToString() + ".png"
                    fileIsAnImage = True
                End If


                If (fileIsAnImage) Then
                    Dim filePath = Request.MapPath("../Pictures/Quiz") + "/" + myFileName
                    fuPictureUrl.SaveAs(filePath)

                    Dim editQuizQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                    editQuizQuestion.UpdateQuizQuestionByQuizQuestionIDWithPicture(Convert.ToInt32(Request.QueryString("QuizQuestionId")), txtQuestion.Text, txtPossibleAnswer1.Text, txtPossibleAnswer2.Text, txtPossibleAnswer3.Text, txtPossibleAnswer4.Text, drpPossibleAnswers.SelectedValue, "Pictures/Quiz/" + myFileName, txtAnswerExplenation.Text)

                    lblMessage.Text = "Update done"
                Else
                    lblMessage.Text = "Only jpeg or png files are allowed"
                End If
            Else

                Dim editQuizQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                editQuizQuestion.UpdateQuizQuestionByQuizQuestionIDWithoutPicture(Convert.ToInt32(Request.QueryString("QuizQuestionId")), txtQuestion.Text, txtPossibleAnswer1.Text, txtPossibleAnswer2.Text, txtPossibleAnswer3.Text, txtPossibleAnswer4.Text, drpPossibleAnswers.SelectedValue, txtAnswerExplenation.Text)
                lblMessage.Text = "Update is done"

            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnAddAnotherQuestion_Click(sender As Object, e As EventArgs) Handles btnAddAnotherQuestion.Click
        Try

            Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
            Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizQuestionId(Convert.ToInt32(Request.QueryString("QuizQuestionId")))
            Dim quizId As Int32 = Convert.ToInt32(Request.QueryString("QuizId"))


            Dim _contextQuizID As HttpContext = HttpContext.Current
            _contextQuizID.Items.Add("QuizId", quizId)
            Server.Transfer("AddQuizQuestions.aspx")


        Catch ex As Exception

        End Try
    End Sub
End Class